package com.qyhl.module_activities.act.player.detail;


import com.google.gson.Gson;
import com.qyhl.module_activities.R;
import com.qyhl.module_activities.common.ActUrl;
import com.qyhl.webtv.basiclib.utils.ContextUtilts;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.constant.ErrorCode;
import com.qyhl.webtv.commonlib.entity.act.PlayerVOBean;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by helin on 2018/1/16.
 */
public class PlayerDetailModel implements PlayerDetailContract.PlayerDetailModel {


    private final PlayerDetailContract.PlayerDetailPresenter mPresenter;

    PlayerDetailModel(PlayerDetailContract.PlayerDetailPresenter playerDetailPresenter) {

        mPresenter = playerDetailPresenter;
    }

    @Override
    public void getPlayerDetail(String playerId) {
        EasyHttp.get(ActUrl.ACTIVITY_PLAYER_DETAIL)
                .params("playerId", playerId)
                .execute(new SimpleCallBack<PlayerVOBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.onError(2, ContextUtilts.getInstance().getmContext().getString(R.string.error_str));
                    }

                    @Override
                    public void onSuccess(PlayerVOBean playerVOBean) {
                        mPresenter.onSuccess(playerVOBean);
                    }
                });
        



    }

    @Override
    public void vote(Integer actId, String phoneNum, Integer playerId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = ActUrl.ACTIVITY_VOTE_ENCRYPTION + "?timestamp=" + time;

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "actId=" + actId + "&userName=" + phoneNum +
                        "&playerId=" + playerId + "&number=1"))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.onError(2, "无网络连接，请检查您的网络...");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult= new Gson().fromJson(s,ApiResult.class);
                        switch (apiResult.getCode()) {
                            case ErrorCode.VOTE_STATUS_1:
                                mPresenter.onError(2, "您已用完今日票数");
                                break;
                            case ErrorCode.VOTE_STATUS_2:
                                mPresenter.onError(2, "本次活动您的票数已用完");
                                break;
                            case ErrorCode.VOTE_STATUS_3:
                                mPresenter.onError(2, apiResult.getMessage());
                                break;
                            case ErrorCode.VOTE_STATUS_4:
                            case ErrorCode.VOTE_STATUS_5:
                                mPresenter.onError(2, "对同一选手只能投一票");
                                break;
                            case ErrorCode.SUCCESS:
                                mPresenter.onVoteSuccess();
                                break;
                            default:
                                mPresenter.onError(2, apiResult.getMessage());
                                break;
                        }
                    }
                });


    }

}
